//
//  User.m
//  MM
//
//  Created by 郑渊文 on 5/21/15.
//  Copyright (c) 2015 王晨辉. All rights reserved.
//

#import "UserModel.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFNetworking.h"
#import "NSDictionary+JsonString.h"
#import "NSString+DictionaryValue.h"
#import "SSKeychain.h"


@implementation UserModel


+ (AFHTTPRequestOperation *)getUser:(NSDictionary *)paramDic
                            success:(void (^)(UserModel *user))success
                             failed:(void (^)(NSError *error))failed{
    
    NSLog(@"paramDic%@",paramDic);
    //直接发送json给服务器端   对应[AFJSONRequestSerializer serializer];
    NSDictionary *param = paramDic;
    
    //将请求参数转换为json后放入key为jsonString的字典中发送请求    对应[AFHTTPRequestSerializer serializer];
    // NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //[parameters setObject:[param dictionaryToJson] forKey:@"jsonString"];

    
    return [[APIClient sharedJsonClient] POST:kNetPath_Code_Login parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"返回的值是%@",responseObject);
        NSInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == 1) {
            if (success) {
                NSArray *myList = responseObject[@"dataList"];
//              UserModel *user =  [[UserModel alloc] initWithDictionary:responseObject error:nil];
                success(responseObject);
            }
        }else
        {
            NSString *str = responseObject[@"msg"][@"j_captcha_error"];
            NSLog(@"=======%@",str);
            if (failed) {
                failed(str);
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failed(error);
    }];
}

//获取验证码
+ (AFHTTPRequestOperation *)getCheckNum:(NSDictionary *)paramDic
                               withBlock:(void (^)(NSString *checkNum, NSError *error))block{
    
    return [[APIClient sharedJsonClient] POST:kNetPath_Code_Check parameters:paramDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *checkNum = @"7777";
        block(checkNum, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block(nil, error);
        }
    }];
};

//注销
+ (void)logoutAccount{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:KEY_USER_NAME];
    [userDefaults synchronize];
}
//获取userdefault用户名 和钥匙串中密码
+ (void)getAccount:(void (^)(NSString *username,NSString *password))block{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *username = [userDefaults objectForKey:KEY_USER_NAME];
    NSString *password = [SSKeychain passwordForService:KEY_KEYCHAIN_SERVICE account:username];
    block(username?:@"",password?:@"");
}
//存储用户名userdefault ,密码到钥匙串
+ (void)saveAccount:(NSString *)name andPassword:(NSString *)password {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:name forKey:KEY_USER_NAME];
    [userDefaults synchronize];
    [SSKeychain setPassword:password forService:KEY_KEYCHAIN_SERVICE account:name];
}

@end
